package com.example.moshudriver.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.BeeFramework.model.BaseModel;
import com.BeeFramework.model.BeeCallback;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.ENUM_PUBLISHED_ORDER_STATE;
import com.example.moshudriver.protocol.ORDER_INFO;
import com.example.moshudriver.protocol.orderlistpublishedRequest;
import com.example.moshudriver.protocol.orderlistpublishedResponse;
import com.example.moshudriver.protocol.orderlistreceivedResponse;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.example.moshudriver.tools.SESSION;
import com.external.androidquery.callback.AjaxStatus;

//发布订单的实体类
public class PublishedOrderListModel extends BaseModel {
	// 完成订单的list集合
	public ArrayList<ORDER_INFO> publicFinishedOrderList = new ArrayList<ORDER_INFO>();
	// 未完成订单的list集合
	public ArrayList<ORDER_INFO> publicUnfinishedOrderList = new ArrayList<ORDER_INFO>();
	private String PUBLISHED_ORDER_LIST_UNFINISH = "published_order_list_unfinsh";
	private String PUBLISHED_ORDER_LIST_FINISH = "published_order_list_finsh";
	// 表示Xlistview初始状态下显示多少行
	public static final int NUMPERPAGE = 10;

	public PublishedOrderListModel(Context context) {
		super(context);
	}

	// 获取到之前未完成的订单
	public void fetchPreUnfinished() {
		orderlistpublishedRequest request = new orderlistpublishedRequest();

		request.sid = SESSION.getInstance().sid;
		request.uid = SESSION.getInstance().uid;
		request.count = NUMPERPAGE;
		request.by_no = 1;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.published_order = ENUM_PUBLISHED_ORDER_STATE.PUBLISHED_ORDER_UNDONE
				.value();

		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					PublishedOrderListModel.this
							.callback(this, url, jo, status);
					if (null != jo) { // 实例化发布的订单列表的响应
						orderlistpublishedResponse response = new orderlistpublishedResponse();
						response.fromJson(jo);
						// 当响应成功的时候
						if (response.succeed == 1) {
							// 清除之前的发布未完成订单的列表
							publicUnfinishedOrderList.clear();
							publicUnfinishedOrderList.addAll(response.orders);
							editor.putString(PUBLISHED_ORDER_LIST_UNFINISH
									+ SESSION.getInstance().uid, jo.toString());
							editor.commit();
							PublishedOrderListModel.this.OnMessageResponse(url,
									jo, status);
						} else {
							PublishedOrderListModel.this.callback(url,
									response.error_code, response.error_desc);
						}
					} else {
						PublishedOrderListModel.this.OnMessageResponse(url, jo,
								status);
					}

				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			JSONObject requestJson = request.toJson();
			requestJson.remove("by_id");
			params.put("json", requestJson.toString());

		} catch (JSONException e) {

		}
		if (isSendingMessage(ApiInterface.ORDERLIST_PUBLISHED)) {
			return;
		}
		cb.url(ApiInterface.ORDERLIST_PUBLISHED).type(JSONObject.class)
				.params(params);
		ajaxProgress(cb);
	}

	// 获取下一个未完成的订单
	public void fetchNextUnfinished() {
		orderlistpublishedRequest request = new orderlistpublishedRequest();

		request.sid = SESSION.getInstance().sid;
		request.uid = SESSION.getInstance().uid;
		request.count = NUMPERPAGE;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.by_no = (int) Math.ceil(publicUnfinishedOrderList.size() * 1.0
				/ NUMPERPAGE) + 1;
		;

		request.published_order = ENUM_PUBLISHED_ORDER_STATE.PUBLISHED_ORDER_UNDONE
				.value();

		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					PublishedOrderListModel.this
							.callback(this, url, jo, status);
					if (null != jo) {
						orderlistpublishedResponse response = new orderlistpublishedResponse();
						response.fromJson(jo);

						if (response.succeed == 1) {
							publicUnfinishedOrderList.addAll(response.orders);
							PublishedOrderListModel.this.OnMessageResponse(url,
									jo, status);
						} else {
							PublishedOrderListModel.this.callback(url,
									response.error_code, response.error_desc);
						}
					} else {
						PublishedOrderListModel.this.OnMessageResponse(url, jo,
								status);
					}

				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			JSONObject requestJson = request.toJson();
			requestJson.remove("by_id");
			params.put("json", requestJson.toString());

		} catch (JSONException e) {

		}
		if (isSendingMessage(ApiInterface.ORDERLIST_PUBLISHED)) {
			return;
		}
		cb.url(ApiInterface.ORDERLIST_PUBLISHED).type(JSONObject.class)
				.params(params);
		ajax(cb);
	}

	// 获取前一个完成的订单
	public void fetchPrefinished() {
		orderlistpublishedRequest request = new orderlistpublishedRequest();

		request.sid = SESSION.getInstance().sid;
		request.uid = SESSION.getInstance().uid;
		request.count = NUMPERPAGE;
		request.by_no = 1;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.published_order = ENUM_PUBLISHED_ORDER_STATE.PUBLISHED_ORDER_DONE
				.value();

		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					PublishedOrderListModel.this
							.callback(this, url, jo, status);
					if (null != jo) {
						orderlistpublishedResponse response = new orderlistpublishedResponse();
						response.fromJson(jo);

						if (response.succeed == 1) {
							publicFinishedOrderList.clear();
							publicFinishedOrderList.addAll(response.orders);
							editor.putString(PUBLISHED_ORDER_LIST_FINISH
									+ SESSION.getInstance().uid, jo.toString());
							editor.commit();
							PublishedOrderListModel.this.OnMessageResponse(url,
									jo, status);
						} else {
							PublishedOrderListModel.this.callback(url,
									response.error_code, response.error_desc);
						}
					} else {
						PublishedOrderListModel.this.OnMessageResponse(url, jo,
								status);
					}

				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			JSONObject requestJson = request.toJson();
			requestJson.remove("by_id");
			params.put("json", requestJson.toString());

		} catch (JSONException e) {

		}
		if (isSendingMessage(ApiInterface.ORDERLIST_PUBLISHED)) {
			return;
		}
		cb.url(ApiInterface.ORDERLIST_PUBLISHED).type(JSONObject.class)
				.params(params);
		ajaxProgress(cb);

	}

	// 获取下一个完成的订单的列表
	public void fetchNextfinished() {
		// 实例化发布订单的列表的请求方法
		orderlistpublishedRequest request = new orderlistpublishedRequest();

		request.sid = SESSION.getInstance().sid;
		request.uid = SESSION.getInstance().uid;
		request.count = NUMPERPAGE;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.by_no = (int) Math.ceil(publicFinishedOrderList.size() * 1.0
				/ NUMPERPAGE) + 1;
		;

		request.published_order = ENUM_PUBLISHED_ORDER_STATE.PUBLISHED_ORDER_DONE
				.value();

		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					PublishedOrderListModel.this
							.callback(this, url, jo, status);
					if (null != jo) {
						orderlistpublishedResponse response = new orderlistpublishedResponse();
						response.fromJson(jo);

						if (response.succeed == 1) {
							publicFinishedOrderList.clear();
							publicFinishedOrderList.addAll(response.orders);
							PublishedOrderListModel.this.OnMessageResponse(url,
									jo, status);
						} else {
							PublishedOrderListModel.this.callback(url,
									response.error_code, response.error_desc);
						}
					} else {
						PublishedOrderListModel.this.OnMessageResponse(url, jo,
								status);
					}

				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			JSONObject requestJson = request.toJson();
			requestJson.remove("by_id");
			params.put("json", requestJson.toString());

		} catch (JSONException e) {

		}
		if (isSendingMessage(ApiInterface.ORDERLIST_PUBLISHED)) {
			return;
		}
		cb.url(ApiInterface.ORDERLIST_PUBLISHED).type(JSONObject.class)
				.params(params);
		ajax(cb);

	}

	// 加载未完成的订单缓存
	public void loadCacheUnFinished() {
		String received_order_list_unfinsh = shared
				.getString(
						PUBLISHED_ORDER_LIST_UNFINISH
								+ SESSION.getInstance().uid, null);
		if (null != received_order_list_unfinsh) {
			try {
				JSONObject jsonObject = new JSONObject(
						received_order_list_unfinsh);
				final orderlistreceivedResponse orderlistreceivedResponse = new orderlistreceivedResponse();
				orderlistreceivedResponse.fromJson(jsonObject);
				if (orderlistreceivedResponse.succeed == 1) {
					publicUnfinishedOrderList.clear();
					publicUnfinishedOrderList
							.addAll(orderlistreceivedResponse.orders);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

	// 加载完成的订单缓存
	public void loadCacheFinished() {
		// 获取从sharePreference上保存的数据
		String received_order_list_finish = shared.getString(
				PUBLISHED_ORDER_LIST_FINISH + SESSION.getInstance().uid, null);
		if (null != received_order_list_finish) {
			try {
				JSONObject jsonObject = new JSONObject(
						received_order_list_finish);
				// 实例化接受订单的响应方法
				final orderlistreceivedResponse orderlistreceivedResponse = new orderlistreceivedResponse();
				orderlistreceivedResponse.fromJson(jsonObject);
				if (orderlistreceivedResponse.succeed == 1) {
					publicFinishedOrderList.clear();
					publicFinishedOrderList
							.addAll(orderlistreceivedResponse.orders);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}
	// 判断是否可以接单  count 大于0就可以接单否则不能接单
	public void isAccept() {
		orderlistpublishedRequest request = new orderlistpublishedRequest();

		request.sid = SESSION.getInstance().sid;
		request.uid = SESSION.getInstance().uid;
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.published_order = 4;//表示没有确定付款的即时订单

		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					PublishedOrderListModel.this
							.callback(this, url, jo, status);
					if (null != jo) { // 实例化发布的订单列表的响应
						orderlistpublishedResponse response = new orderlistpublishedResponse();
						response.fromJson(jo);
						// 当响应成功的时候
						if (response.succeed == 1) {
							
							PublishedOrderListModel.this.OnMessageResponse(url,
									jo, status);
						} else {
							PublishedOrderListModel.this.callback(url,
									response.error_code, response.error_desc);
						}
					} else {
						PublishedOrderListModel.this.OnMessageResponse(url, jo,
								status);
					}

				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			JSONObject requestJson = request.toJson();
			requestJson.remove("by_id");
			params.put("json", requestJson.toString());

		} catch (JSONException e) {

		}
		
		cb.url(ApiInterface.ORDERLIST_ISACCEPT).type(JSONObject.class)
				.params(params);
		ajaxProgress(cb);
	}
	
	
}
